﻿using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace Job_Assignment
{
    class EmployeeAssignment
    {   
        DataTable _plan;
        DataTable _empAndSkillList;
        DataTable _WstAndSkillList;

        public EmployeeAssignment(DataTable plan, DataTable empList, DataTable wstAndSkillLst)
	    {
            _plan = plan;
            _empAndSkillList = empList;
            _WstAndSkillList = wstAndSkillLst;
	    }

        public DataTable GetFinalList()
        {
            if (_plan == null || _empAndSkillList == null || _WstAndSkillList == null)
            {
                return null;
            }

            string workStationID, lineID;
            string emp_ID = "";
            string emp_Name = "";

            foreach (DataRow row in _plan.Rows)
            {
                workStationID = row["WST_ID"].ToString().Trim();

                if (GetEmployeeForThisWST(ref emp_ID, ref emp_Name, workStationID))
                {
                    row["Empl_ID"] = emp_ID;
                    row["Empl_Name"] = emp_Name;
                }
            }

            return _plan;
        }

        private bool GetEmployeeForThisWST(ref string empl_ID, ref string empl_Name, string workStationID)
        {
            if (_plan == null || _empAndSkillList == null || _WstAndSkillList == null)
            {
                return false;
            }

            if (workStationID == string.Empty)
            {
                return false;
            }

            //Find Skill ID
            List<string> list_SkillID_ForThisWST = GetSkillListForThisWST(workStationID);
            List<string> list_SkillID_OfThisEmp;

            DataTable listOfEmp = GetListOfEmployee();


            foreach (DataRow item in listOfEmp.Rows)
            {
                string ID = item["Empl_ID"].ToString().Trim();
                string name = item["Empl_Name"].ToString().Trim();
                list_SkillID_OfThisEmp = GetSkillListOfThisEmpl(ID);

                if (list_SkillID_OfThisEmp == null)
                {
                    continue;
                }

                if (CompareSkill_vs_RequiredSkill(ID, name, list_SkillID_OfThisEmp, list_SkillID_ForThisWST))
                {
                    empl_Name = name;
                    empl_ID = ID;

                    //remove this imployee from the list
                    List<DataRow> rowsToDelete = new List<DataRow>();

                    foreach (DataRow row in _empAndSkillList.Rows)
                    {
                        if (row["Empl_ID"].ToString().Trim() == empl_ID)
                        {
                            rowsToDelete.Add(row);
                        }
                    }

                    foreach (DataRow row in rowsToDelete)
                    {
                        _empAndSkillList.Rows.Remove(row);
                    }

                    //empl_vs_skill_tbl.AcceptChanges();
                    return true;
                }
                //return false;
            }

            return true;
        }

        private DataTable GetListOfEmployee()
        {
            if (_empAndSkillList == null)
            {
                return null;
            }

            DataView view = new DataView(_empAndSkillList);
            DataTable distinctValues = view.ToTable(true, "Empl_ID", "Empl_Name");

            return distinctValues;
        }

        private bool ContainsAllItems(List<string> a, List<string> b)
        {
            return !b.Except(a).Any();
        }

        private bool CompareSkill_vs_RequiredSkill(string empl_ID, string empl_Name, List<string> listSkillOfThisEmpl, List<string> RequiredSkill)
        {
            if (listSkillOfThisEmpl == null || RequiredSkill == null)
            {
                return false;
            }

            if (ContainsAllItems(listSkillOfThisEmpl, RequiredSkill))
            {
                return true;
            }

            return false;
        }

        private List<string> GetSkillListForThisWST(string wst_ID)
        {
            if (wst_ID == string.Empty)
	        {
                return null;        		 
	        }

            List<string> lst = new List<string>();
            string skillID;

            DataRow[] results = _WstAndSkillList.Select("WorkStationID = '" + wst_ID + "'");

            if (results.Length > 0)
            {
                foreach (DataRow item in results)
                {
                    skillID = item["SkillID"].ToString();
                    lst.Add(skillID);
                }
            }

            return lst;
        }

        private List<string> GetSkillListOfThisEmpl(string emp_ID)
        {
            if (emp_ID == string.Empty)
            {
                return null;
            }

            List<string> lst = new List<string>();
            string skillID;

            DataRow[] results = _empAndSkillList.Select("Empl_ID = '" + emp_ID + "'");

            if (results.Length > 0)
            {
                foreach (DataRow item in results)
                {
                    skillID = item["Skill_ID"].ToString();
                    lst.Add(skillID);
                }
            }

            return lst;
        }


    }
}
